<?php
Prado::using('Application.common.common');
class Register extends TPage
{
	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->Master->pageHead->Title = $this->Application->Parameters["WEBSITE_TITLE"] . " | Register Account";
		
		if (!$this->IsPostBack)
		{			
			if ($this->Request->contains('m')) {
				if ($this->Request['m'] == 'cu') {
					$this->Notice->Type = UserNoticeType::Notice;
					$this->Notice->Text = "<p style='text-align:center;'>Your account has been registered successfully.</p>";			
				}
				
				$this->mainPanel->Visible = false;
				$this->btnContinue->Visible = true;
			}
		}
	}	
	
	protected function btnSubmit_Clicked($sender, $param)
	{
		if ($this->IsValid)
		{
			try
			{
				$userRecord = new UserRecord;
				$userRecord->Username = $this->txtEmail->SafeText;
				$userRecord->Password = md5($this->txtPassword->SafeText);							
				$userRecord->FirstName = $this->txtFirstName->SafeText;
				$userRecord->LastName = $this->txtLastName->SafeText;	
				$userRecord->Gender = $this->rbGender->SelectedValue;
				$userRecord->Email = $this->txtEmail->SafeText;
				$userRecord->Address = $this->txtAddress->SafeText;
				$userRecord->City = $this->txtCity->SafeText;	
				$userRecord->Country = $this->ddlCountry->SelectedValue;
				$userRecord->PostalCode = $this->txtPostalCode->SafeText;					
				$userRecord->Phone = $this->txtPhone->SafeText;					
				$userRecord->Phone2 = $this->txtPhone2->SafeText;					
				$userRecord->Newsletter = $this->rbNewsletterType->SelectedValue;				
				$userRecord->Status = 1; // status: 0 inactive, 1 active, 2 admin -1 deleted
				$userRecord->Hash = md5(uniqid(time())).'.'.strtolower(array_pop(explode('.',$this->txtEmail->SafeText)));
				$userRecord->IPAddress = $this->Request->UserHostAddress;
				$userRecord->CreateBy = $userRecord->ModifyBy = 0; // 0: self created	
				$userRecord->CreateDate = $userRecord->ModifyDate = time();								
				$userRecord->save();
				
				// if register successsful, send email
				/*$emailer = $this->Application->getModule('mailer');
				$email = $emailer->createNewEmail("RegisterSuccess");
				$email->HtmlContent->findControl("USER_NAME")->Text = $userRecord->UserName;				
				$receiver = new TEmailAddress;
				$receiver->Field = TEmailAddressField::Receiver;
				$receiver->Address = $userRecord->Email;
				$receiver->Name = $userRecord->UserName;
				$email->getEmailAddresses()->add($receiver);
				$emailer->send($email);	*/
								
				$this->Application->getModule("auth")->login($this->txtEmail->SafeText,$this->txtPassword->SafeText);
				$this->Response->redirect($this->Service->ConstructUrl("account.Register", array('m'=>'cu')));										
			}
			catch(TException $e)
			{
				$this->Notice->Type = UserNoticeType::Error;
				$this->Notice->Text = $e;
			}
		}
	}
	
	protected function uniqueCheck_ServerValidated($sender, $param)
	{
		if ($param->Value != '')
		{
			$criteria = new TActiveRecordCriteria;
			$criteria->Condition = "user_email = :email";
			$criteria->Parameters[":email"] = $param->Value;
			$param->IsValid = !(UserRecord::finder()->find($criteria) instanceof UserRecord);
		}
	}
	
	protected function btnContinue_Clicked($sender, $param)
	{
		$this->Response->redirect($this->Service->ConstructUrl("Index"));
	}
}

?>